package com.banciyuan.bcywebview.biz.ask.ask;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper.OnActionCallbacks;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.NetUtils;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

/**
 * @author hfxt2008
 *         <p/>
 *         首页个人页面/个人空间页面/ASK卡片->ASK页面->提问页面
 */
public class WriteAskActivity extends BaseActivity implements OnActionCallbacks {

    private PushActionInterface mInterface;

    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;
    private EditText mContentEt;
    private CheckBox mNoNameCb;
    private RelativeLayout mNoNameRly;
    private WriteAskHelper mHelper;
    private TextView tv_save;
    private ProgressDialog mDialog;
    private Boolean CLICK_FLAG = false;
    private String ouid = "";
    private String isAnonymous = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_ask_layout);
        initArgs();
        initActionbar();
        initUi();
        initAction();
    }

    @Override
    protected void initArgs() {
        if (!UserDataHelper.ifLogin(this) || TextUtils.isEmpty(UserDataHelper.getInstance(this).getToken())) {
            MyToast.show(this, getString(R.string.loginfirst));
            finish();
        }
        mInterface = new PushActionInterface() {
            @Override
            public void onNetError(String result) {
                onNetworkError(result);
            }

            @Override
            public void PushSuccess(String result) {
                pushSuccess(result);
            }
        };
        ouid = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_ONE);
        isAnonymous = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_TWO);
        mHelper = new WriteAskHelper(this, mInterface, ouid);
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle("");
        mActionbarHelper.addItemOneText(getString(R.string.question));
        mActionbarHelper.setActionCallbacks(this);
        tv_save = (TextView) findViewById(R.id.base_action_bar_home_text_item);
        tv_save.setTextColor(getResources().getColorStateList(R.color.font_color));
    }

    @Override
    protected void initAction() {
        mContentEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.length() == 0) {
                    tv_save.setTextColor(getResources().getColorStateList(R.color.font_color));
                    CLICK_FLAG = false;
                    return;
                } else if (editable.toString().contains("?") && editable.toString().contains("？")) {
                    tv_save.setTextColor(getResources().getColorStateList(R.color.font_color));
                    CLICK_FLAG = true;
                    return;
                }
                tv_save.setTextColor(getResources().getColorStateList(R.color.pink));
                CLICK_FLAG = true;
            }
        });
    }

    @Override
    protected void initUi() {
        mContentEt = (EditText) findViewById(R.id.write_ask_content_et);
        mContentEt.requestFocus();
        mNoNameCb = (CheckBox) findViewById(R.id.write_ask_noname_cb);
        mNoNameRly = (RelativeLayout) findViewById(R.id.write_ask_noname_rly);
        if (StringUtil.notNullEndWith("1", isAnonymous)) mNoNameRly.setVisibility(View.VISIBLE);
        else mNoNameRly.setVisibility(View.GONE);

        mDialog = new ProgressDialog(this);
        mDialog.setMessage(this.getResources().getString(R.string.all_send_message));
    }

    @Override
    public void onBackClick() {
        finish();
    }

    @Override
    public void onActionClick(int id) {
        switch (id) {
            case R.id.base_action_bar_home_text_item:
                if (CLICK_FLAG) {
                    String content = mContentEt.getText().toString();
                    if (TextUtils.isEmpty(content)) {
                        MyToast.show(this, getString(R.string.noemptycotent));
                        return;
                    }
                    if (!content.contains("?") && !content.contains("？")) {
                        MyToast.show(this, getString(R.string.question_mark_first));
                        return;
                    }
                    if (!NetUtils.checkNetwork(this)) {
                        MyToast.show(this, getString(R.string.net_check_first));
                        return;
                    }
                    mDialog.show();
                    mHelper.setContent(mContentEt.getText().toString());
                    mHelper.doPush(mNoNameCb.isChecked());
                }

                break;
            default:
                break;
        }
    }

    public void onNetworkError(String result) {
        mDialog.dismiss();
        MyToast.show(this, result);
    }

    public void pushSuccess(String result) {
        mDialog.dismiss();
        MyToast.show(this, getString(R.string.question_succ));
        finish();
    }
}